﻿using System;
using System.ComponentModel.DataAnnotations;
using System.Web;
using System.Drawing;
using System.Drawing.Imaging;
using System.Globalization;

namespace WebSiteFool
{
    [AttributeUsage(AttributeTargets.Property, AllowMultiple = true, Inherited = true)]
    public class FileValidationAttribute : ValidationAttribute
    {
        public override bool IsValid(object value)
        {
            var file = value as HttpPostedFileBase;
            if (file == null || file.ContentLength > 1024 * 1024)
            {
                return false;
            }
            if (file.FileName.EndsWith(".jpg", true, CultureInfo.InvariantCulture) ||
                file.FileName.EndsWith(".png", true, CultureInfo.InvariantCulture) ||
                file.FileName.EndsWith(".gif", true, CultureInfo.InvariantCulture) ||
                file.FileName.EndsWith(".bmp", true, CultureInfo.InvariantCulture))
            {
                return true;
            }
            return false;
        }
    }
}